package cn.hxzy.company.service.impl;

import cn.hxzy.company.dao.WorkDao;
import cn.hxzy.company.entity.News;
import cn.hxzy.company.entity.Work;
import cn.hxzy.company.entity.vo.PageBean;
import cn.hxzy.company.service.WorkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Work)表服务实现类
 *
 * @author makejava
 * @since 2020-08-03 22:29:58
 */
@Service("workService")
public class WorkServiceImpl implements WorkService {
    @Resource
    private WorkDao workDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Work queryById(Integer id) {
        return this.workDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Work> queryAllByLimit(int offset, int limit) {
        return this.workDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param work 实例对象
     * @return 实例对象
     */
    @Override
    public Work insert(Work work) {
        this.workDao.insert(work);
        return work;
    }

    /**
     * 修改数据
     *
     * @param work 实例对象
     * @return 实例对象
     */
    @Override
    public Work update(Work work) {
        this.workDao.update(work);
        return this.queryById(work.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.workDao.deleteById(id) > 0;
    }

    /**
     * 查询全部信息
     * @return
     */
    @Override
    public List<Work> queryAll() {
        return workDao.findAll();
    }

    /**
     * 内容分页
     * @param pageBean
     */
    @Override
    public void queryPage(PageBean pageBean) {
        Integer size = pageBean.getSize();
        Integer page = pageBean.getPage();
        List<Work> works = workDao.queryAllByLimit((page - 1) * size, size);
        pageBean.setList(works);//list 对象集合
        long count=workDao.countAll();
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count%size==0?(count/size):(count/size+1)));
    }

    /**
     * 模糊查询->id
     * @param pageBean
     * @param id
     */
    @Override
    public void queryPageWithId(PageBean pageBean, String id) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<Work> works = workDao.queryLimitWithId("%" + id + "%", (page - 1) * size, size);
        pageBean.setList(works);
        long count = workDao.countId("%" + id + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }

    /**
     * 模糊查询->users
     * @param pageBean
     * @param users
     */
    @Override
    public void queryPageWithUsers(PageBean pageBean, String users) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<Work> works = workDao.queryLimitWithUsers("%" + users + "%", (page - 1) * size, size);
        pageBean.setList(works);
        long count = workDao.countUsers("%" + users + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }

    /**
     * 模糊查询->name
     * @param pageBean
     * @param name
     */
    @Override
    public void queryPageWithName(PageBean pageBean, String name) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<Work> works = workDao.queryLimitWithName("%" + name + "%", (page - 1) * size, size);
        pageBean.setList(works);
        long count = workDao.countName("%" + name  + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }

    @Override
    public void queryPageWithDate(PageBean pageBean, String date) {
        Integer page = pageBean.getPage();
        Integer size = pageBean.getSize();
        List<Work> works = workDao.queryLimitWithDate("%" + date + "%", (page - 1) * size, size);
        pageBean.setList(works);
        long count = workDao.countDate("%" + date  + "%");
        pageBean.setCount(count);
        pageBean.setPageCount((int) (count % size == 0 ? (count / size) : (count / size + 1)));
    }
}